<template>
  <div class="app-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="供应商简介" prop="supplierDesc">
          <el-input v-model="form.supplierDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="邮编" prop="zip">
          <el-input v-model="form.zip" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="供应商地址" prop="addr">

    <el-row>
      <el-col :span="4">
            <el-cascader
              v-model="selectedArea"
              :options="areaOptions"
              :props="{ expandTrigger: 'hover' }"
              placeholder="请选择省/市/区"
              @change="handleAreaChange"
              :span="8"
            ></el-cascader>
      </el-col>
      <el-col :span="20">
            <el-input v-model="detailedInfo" placeholder="请输入详细信息" :span="8"></el-input>
    </el-col>
  </el-row>

        </el-form-item>


      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="handleUpdate">重置</el-button>
      </el-form-item>
      </el-form>

  </div>
</template>

<script>
import { getSupplier, updateSupplier } from "@/api/system/supplier";
import user from "@/store/modules/user";
import { pcaTextArr } from 'element-china-area-data'
export default {
  name: "Supplier",
  data() {
    return {
      selectedArea: [],
      detailedInfo: '',
      areaOptions: pcaTextArr,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        supplierDesc: [
          { required: true, message: "供应商简介不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          { pattern: /^1\d{10}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ],
        zip: [
          { required: true, message: "邮编不能为空", trigger: "blur" },
          { pattern: /^\d{6}$/, message: "请输入正确的邮编", trigger: "blur" }
        ],
        addr: [
          { required: true, message: "供应商地址不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.handleUpdate()
  },
    computed: {
      combinedData() {
        if (this.selectedArea.length === 3) {
          return this.selectedArea[0]+'.'+this.selectedArea[1]+'.'+this.selectedArea[2]+'|${this.detailedInfo}'
        } else {
          return null
        }
      }
    },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        supplierNo: null,
        supplierName: null,
        supplierDesc: null,
        userId: null,
        telephone: null,
        zip: null,
        addr: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    setSelectedArea(input) {
      // 先根据 | 分出两个字符串
      const [address, code] = input.split("|");
      this.selectedArea = address.split(".");
      this.detailedInfo = code;
    },
    /** 修改按钮操作 */
    handleUpdate() {
      // this.reset();
      const supplierId =  user.state.id
      getSupplier(supplierId).then(response => {
        this.form = response.data;
        this.setSelectedArea(this.form.addr)
      });
    },
        handleAreaChange(value) {
          this.selectedArea = value
        },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

            this.form.addr = this.combinedData;
            if (this.form.addr==null){
              this.$modal.msgSuccess("地址不合法");
              return;
            }
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
            });
        }
      });
    }
  }
};
</script>
